/*
* @(#)ListDmsCheckOutDocumentAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.dms.web;

import java.sql.Connection;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.dao.DmsCheckoutHistoryDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;

/**
 * <p>Class Name:       ListDmsCheckOutDocumentAction.java    </p>
 * <p>Description:      The list action class for ListDmsCheckOutDocument.jsp</p>
 *
 *    @author           Jenny Li
 *    @company          DCIVision Limited
 *    @creation date    14/03/2005
 *    @version          $Revision: 1.4.2.3 $
 */

public class ListDmsCheckOutDocumentAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.4.2.3 $";


  /**
   *  Constructor - Creates a new instance of ListArchiveAction and define the default listName.
   */
  public ListDmsCheckOutDocumentAction() {
    super();
    this.setListName("checkOutDocumentList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsCheckoutHistoryDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_PUBLIC_FOLDER);
  }
  
  /**
   * setPageTitle
   * set the extend page title and page path.
   * default page path/title will be created by navmode and functionCode
   */
  public void setPageTitle(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping, ActionForward actionForward){
    String extendTitle = MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale(), "dms.label.check_out_document");
    request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE, extendTitle);
    request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE_SHOW_ONLY, new Boolean(true));
  }
  
  /**
   * getListData
   *
   * Override the parent's function. Purpose in create the default personal folder when non-exists,
   * and load the dmsDocument list.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    DmsCheckoutHistoryDAObject dmsCheckoutHistoryDAO=new DmsCheckoutHistoryDAObject(sessionContainer,conn);
    List result=new ArrayList();
    result=dmsCheckoutHistoryDAO.getCheckoutListSafety(form);
    int checkoutExpireDay=TextUtility.parseInteger(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_CHECKOUT_EXPIRE_DAY));
    for(int i=0;i<result.size();i++){
      DmsDocument dmsDocument=(DmsDocument)result.get(i);
      Timestamp checkInDate = Utility.addDay(dmsDocument.getCheckoutDate(),checkoutExpireDay);
      dmsDocument.setCheckinDate(checkInDate);  
    }
    request.setAttribute(this.getListName(),result);
    
  }
  
  public static java.sql.Timestamp parseTimestamp(String timeStr) {
    if (timeStr==null) {
      return null;
    }
    try {
      java.util.Date date;
      SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      try {
        date =simpleDateFormat.parse(timeStr);
      } catch (java.text.ParseException e1) {
        date =simpleDateFormat.parse(timeStr);
      }
      return new java.sql.Timestamp(date.getTime());
    } catch (java.text.ParseException e) {
      return null;
    }
  }
  
}
